
/********************************************************************************************************
*
* File                : MSG.C
*
*********************************************************************************************************/

/* Includes ------------------------------------------------------------------*/
#include "stm32f10x.h"
#include "common.h"
#include "msg.h"


#include "stdio.h"
#include <stdarg.h>
#include <string.h>

/**********************************************************************
    Type def
**********************************************************************/
#define MSG_BUFF_SIZE   200

#define MSG_OUTPUT_PATH         USART1

/**********************************************************************
    GLOBAL Variable
**********************************************************************/
static char msg_str[MSG_BUFF_SIZE];




/**********************************************************************
    Debug msg
**********************************************************************/

/*------------------------------------------------------------------------------
 Function Name  : msg
-------------------------------------------------------------------------------*/
void output_string(char* pString, UINT16 len)
{

      for(UINT16 i=0; i<len; i++)
      {

          /* Wait until end of transmit */
          while(USART_GetFlagStatus(MSG_OUTPUT_PATH, USART_FLAG_TXE) == RESET)
          {
          }
          /* Write one byte in the USARTy Transmit Data Register */
          USART_SendData(MSG_OUTPUT_PATH, pString[i]);
          
      }

    
}







/*------------------------------------------------------------------------------
 Function Name  : msg
-------------------------------------------------------------------------------*/
void msg(UINT8 mode, const char * filename, int line, const char * xFormat,...)
{
    UINT16 len;
    va_list arg;


    va_start (arg, xFormat);

    len = vsprintf((char *)msg_str, xFormat,arg);

    va_end (arg);




    switch(mode)
    {
        case CR_MSG:
            output_string(msg_str,len);
            output_string("\r\n",sizeof("\r\n"));
        break;

        case FILE_LINE_CR_MSG:
        {
            char temp_str[100];
            UINT8 temp_len;
            temp_len= sprintf(temp_str,"[%s(%d)]",filename,line);
            output_string(temp_str,temp_len);
            output_string(msg_str,len);
            output_string("\r\n",sizeof("\r\n"));
        }
        break;
        
        case N_MSG:
        default:
            output_string(msg_str,len);
        break;
    }



}





















